<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_email=new email();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["id_mail"])){$_email->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["distinataire"])){$_email->set_distinataire($_POST["distinataire"]);}
		if(isset($_POST["sender"])){$_email->set_sender($_POST["sender"]);}
		if(isset($_POST["date_envoi"])){$_email->set_date_envoi($_POST["date_envoi"]);}
		if(isset($_POST["heur_envoi"])){$_email->set_heur_envoi($_POST["heur_envoi"]);}
		if(isset($_POST["sujet"])){$_email->set_sujet($_POST["sujet"]);}
		if(isset($_POST["objet"])){$_email->set_objet($_POST["objet"]);}
		if(isset($_POST["auteur"])){$_email->set_auteur($_POST["auteur"]);}
		if(isset($_POST["message"])){$_email->set_message($_POST["message"]);}
		if(isset($_POST["etat_sended"])){$_email->set_etat_sended($_POST["etat_sended"]);}
		if(isset($_POST["phase"])){$_email->set_phase($_POST["phase"]);}
		if(isset($_POST["type_destintinataire"])){$_email->set_type_destintinataire($_POST["type_destintinataire"]);}
		$res=$_email->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["id_mail"])){
		$_email->set_id_mail($_POST["id_mail"]);
		$res=$_email->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["id_mail"])){$_email->set_id_mail($_POST["id_mail"]);}
		if(isset($_POST["distinataire"])){$_email->set_distinataire($_POST["distinataire"]);}
		if(isset($_POST["sender"])){$_email->set_sender($_POST["sender"]);}
		if(isset($_POST["date_envoi"])){$_email->set_date_envoi($_POST["date_envoi"]);}
		if(isset($_POST["heur_envoi"])){$_email->set_heur_envoi($_POST["heur_envoi"]);}
		if(isset($_POST["sujet"])){$_email->set_sujet($_POST["sujet"]);}
		if(isset($_POST["objet"])){$_email->set_objet($_POST["objet"]);}
		if(isset($_POST["auteur"])){$_email->set_auteur($_POST["auteur"]);}
		if(isset($_POST["message"])){$_email->set_message($_POST["message"]);}
		if(isset($_POST["etat_sended"])){$_email->set_etat_sended($_POST["etat_sended"]);}
		if(isset($_POST["phase"])){$_email->set_phase($_POST["phase"]);}
		if(isset($_POST["type_destintinataire"])){$_email->set_type_destintinataire($_POST["type_destintinataire"]);}
		$res=$_email->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["id_mail"]) && !empty($_POST["id_mail"])){
		$constraint.=" AND id_mail='".$_POST["id_mail"]."'";
	}

	if(isset($_POST["distinataire"]) && !empty($_POST["distinataire"])){
		$constraint.=" AND distinataire='".$_POST["distinataire"]."'";
	}

	if(isset($_POST["sender"]) && !empty($_POST["sender"])){
		$constraint.=" AND sender='".$_POST["sender"]."'";
	}

	if(isset($_POST["date_envoi"]) && !empty($_POST["date_envoi"])){
		$constraint.=" AND date_envoi='".$_POST["date_envoi"]."'";
	}

	if(isset($_POST["heur_envoi"]) && !empty($_POST["heur_envoi"])){
		$constraint.=" AND heur_envoi='".$_POST["heur_envoi"]."'";
	}

	if(isset($_POST["sujet"]) && !empty($_POST["sujet"])){
		$constraint.=" AND sujet='".$_POST["sujet"]."'";
	}

	if(isset($_POST["objet"]) && !empty($_POST["objet"])){
		$constraint.=" AND objet='".$_POST["objet"]."'";
	}

	if(isset($_POST["auteur"]) && !empty($_POST["auteur"])){
		$constraint.=" AND auteur='".$_POST["auteur"]."'";
	}

	if(isset($_POST["message"]) && !empty($_POST["message"])){
		$constraint.=" AND message='".$_POST["message"]."'";
	}

	if(isset($_POST["etat_sended"]) && !empty($_POST["etat_sended"])){
		$constraint.=" AND etat_sended='".$_POST["etat_sended"]."'";
	}

	if(isset($_POST["phase"]) && !empty($_POST["phase"])){
		$constraint.=" AND phase='".$_POST["phase"]."'";
	}

	if(isset($_POST["type_destintinataire"]) && !empty($_POST["type_destintinataire"])){
		$constraint.=" AND type_destintinataire='".$_POST["type_destintinataire"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_email->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_email->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>